<?php

/**
 * 会员系统功能
 */
class ToolAction extends HomeBaseAction {
    
    public function __construct(){

        parent::__construct();

    }

    /**
     * 找回密码
     * @return [type] [description]
     */
    public function forget(){
        $this->display(':forget');
    }

    /**
     * 处理找回密码邮箱检测
     * @return [type] [description]
     */
    public function handleForget(){
    	$map['email'] = I('post.email');
    	$member_info = M('member')->where($map)->find();
    	if(empty($member_info)){
    		$this->error('该邮箱不存在');
    	}else{
    		// 查询
    		$Forget = M('member_forget_pwd');
    		$where['uid'] = $member_info['id'];
    		$info = $Forget->where($where)->find();
    		if(!empty($info)){
    			// 已经存在记录
    			// if( (time() - $info['create_time']) < 5*60){
    			// 	$this->error('5分钟内不能重复发送');
    			// }
    		}else{
    			// 不存在记录
    			$data['uid'] = $member_info['id'];
    			$data['create_time'] = time();
    			$res = $Forget->add($data);
    			if(!$res){
    				$this->error('添加记录失败');
    			}
    		}

    		$save_data['token'] = md5($member_info['id'].$member_info['account'].$member_info['password'].time()); // 可以再加一个key
    		$save_data['create_time'] = time();
    		$res = $Forget->where($where)->save($save_data);
    		if($res){
    			$link = SITE_URL.U('Tool/setNewPwd',array('token'=>$save_data['token']));
		        $title = 'missql 密码找回邮件';
		        $message = '<a href="'.$link.'">'.$link.'</a>';
		        $status = SendMail(I('post.email'),$title,$message);
		        if($status){
		        	$this->success('密码找回邮件发送成功');
		        }else{
		        	$this->error('邮件发送失败');
		        }
    		}else{
    			$this->error('更新记录失败');
    		}
    	}

    }

    /**
     * 设置新密码
     */
    public function setNewPwd(){
    	$map['token'] = I('get.token');
    	$Forget = M('member_forget_pwd');
    	$info = $Forget->where($map)->find();
    	if(empty($info)){
    		// $this->error('Token已失效或不存在');
            alert('Token已失效或不存在'); 
    	}else{
            $Forget->where($map)->setField('token','');
    		$member_info = M('member')->where('id = '.$info['uid'])->find();
    		session('forget_uid',$member_info['id']);
    		$this->assign('member_info',$member_info);
    		$this->display(':set_new_pwd');
    	}
    }

    /**
     * 处理设置新密码
     * @return [type] [description]
     */
    public function handleSetNewPwd(){
        $uid = session('forget_uid');
        if(empty($uid)){
            $this->error('访问错误');
        }else{
            if(empty($_POST['new_password'])){
                $this->error('密码不能为空');
            }
            if(strlen($_POST['new_password']) < 5){
                $this->error('密码长度必须大于4');
            }
            if($_POST['new_password'] != $_POST['repeat_password']){
                $this->error('密码不一致');
            }else{
                $res = M('member')->where('id = '.$uid)->setField('password',md5($_POST['new_password']));
                if($res){
                    M('member_forget_pwd')->where('uid = '.$uid)->setInc('num');
                    $this->success('新密码设置成功',U('Index/login'));
                }else{
                    $this->error('设置失败');
                }
            }
        }
    }

}

